﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoicePhone.Common;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace VoicePhone.Utils
{
    public class NumberThinConfig : EntityClassXmlSerializer<NumberThinConfig>
    {
        public List<IpPhone> IpPhoneList { get; set; }

        public static NumberThinConfig Load()
        {
            NumberThinConfig numberThinConfig;
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config\\NumberThinConfig.xml");
            if (File.Exists(filePath))
            {
                string xmlStr = File.ReadAllText(filePath);
                numberThinConfig = ReadFromXML(xmlStr);
            }
            else
            {
                if(!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config")))
                    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"));
                numberThinConfig = new NumberThinConfig();
            }

            if (numberThinConfig.IpPhoneList == null) numberThinConfig.IpPhoneList = new List<IpPhone>();
            if (numberThinConfig.IpPhoneList.Count < 72)
            {
                for (int i = 0; i < 72; i++)
                {
                    if (numberThinConfig.IpPhoneList.Count > i)
                        numberThinConfig.IpPhoneList[i].Number = i + 1;
                    else
                        numberThinConfig.IpPhoneList.Add(new IpPhone() { Number = i + 1 });
                }
            }
            return numberThinConfig;
        }

        public void Save()
        {
            string xmlStr = ToXMLString(this);
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config\\NumberThinConfig.xml");
            File.WriteAllText(filePath, xmlStr);
        }
    }

    public class IpPhone : INotifyPropertyChanged
    {
        public int Number { get; set; }

        private string m_CallName;
        public string CallName
        {
            get { return m_CallName; }
            set
            {
                this.m_CallName = value;
                this.OnPropertyChanged();
            }
        }

        private string m_CallNo1;
        public string CallNo1
        {
            get { return m_CallNo1; }
            set
            {
                this.m_CallNo1 = value;
                this.OnPropertyChanged();
            }
        }

        private bool m_Concentration;
        public bool Concentration
        {
            get { return m_Concentration; }
            set
            {
                this.m_Concentration = value;
                this.OnPropertyChanged();
            }
        }
        //public string CallNo2 { get; set; }
        //public string CallNo3 { get; set; }
        //public string CallNo4 { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName]string property = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
        }
    }
}
